package com.healthcloud.task;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.healthcloud.dto.res.PatientInstrumentDto;
import com.healthcloud.service.InstrumentInfoService;
import com.healthcloud.service.PpPatientBaseService;
import com.healthcloud.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 首页 定时任务进入缓存
 */
@Component
public class IndexTask {

    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private PpPatientBaseService ppPatientBaseService;
    @Autowired
    private InstrumentInfoService instrumentInfoService;

    @Scheduled(cron="0 0 */1 * * ? ")   //每小时整点执行一次
    public void indexCount1(){
        String ids = "10000032,10000033,10000034,10000035,10000036";

        //血压
        Integer bpTestNum = ppPatientBaseService.countDiseasesByIdsTest(ids, "104001,104002", 7);
        redisUtils.set("bpTestNum1", String.valueOf(bpTestNum));
        //血糖
        Integer bsTestNum = ppPatientBaseService.countDiseasesByIdsTest(ids, "105001,105002", 7);
        redisUtils.set("bsTestNum1", String.valueOf(bsTestNum));
        //服务人数
        Integer patientTotal = ppPatientBaseService.totalByHospitalIds(ids);
        redisUtils.set("patientTotal", String.valueOf(patientTotal));

        Integer useTimes5 = instrumentInfoService.useTimesByHospitalIds(ids, 5);
        redisUtils.set("useTimes5", String.valueOf(useTimes5));
        Integer useTimes10 = instrumentInfoService.useTimesByHospitalIds(ids, 10);
        redisUtils.set("useTimes10", String.valueOf(useTimes10));
        Integer useTimes15 = instrumentInfoService.useTimesByHospitalIds(ids, 15);
        redisUtils.set("useTimes15", String.valueOf(useTimes15));

        List<PatientInstrumentDto> unUseInfo5List = instrumentInfoService.listUnUseInstrument(ids, 5);
        redisUtils.set("unUseInfo5List", String.valueOf(unUseInfo5List.size()));
        List<PatientInstrumentDto> unUseInfo10List = instrumentInfoService.listUnUseInstrument(ids, 10);
        redisUtils.set("unUseInfo10List", String.valueOf(unUseInfo10List.size()));
        List<PatientInstrumentDto> unUseInfo15List = instrumentInfoService.listUnUseInstrument(ids, 15);
        redisUtils.set("unUseInfo15List", String.valueOf(unUseInfo15List.size()));

    }

    @Scheduled(cron="0 0 */1 * * ? ")   //每小时整点执行一次
    public void indexCount3(){
        String ids = "10000032,10000033,10000034,10000035,10000036";

        // 血压未达标总数
        // Integer bpTestErrorNum = ppPatientBaseService.countDiseasesByIdsTest(ids, "104001,104002", null);
        // 2020-07-06 修改统计标准为：血压未达标总数：以最近一次测量的血压值为标准，大于等于135/85算未达标。135或85有一个指标达到就算未达标
        Integer bpTestErrorNum = ppPatientBaseService.countBpErrorNumByIdsTest(ids);
        redisUtils.set("bpTestErrorNum", String.valueOf(bpTestErrorNum));

        Integer bpTestNum = ppPatientBaseService.coutByIdsTest(ids, "104001,104002");
        redisUtils.set("bpTestNum2", String.valueOf(bpTestNum));

        // 餐前血糖未达标总数
        // Integer bsBeforeTestErrorNum = ppPatientBaseService.countDiseasesByIdsTest(ids, "105001", null);
        List bsBeforeTestErrorList = ppPatientBaseService.listErrorBsIdCards(ids, "105001", 7);
        Integer bsBeforeTestErrorNum = CollUtil.isNotEmpty(bsBeforeTestErrorList) ? bsBeforeTestErrorList.size() : 0;
        redisUtils.set("bsBeforeTestErrorNum", String.valueOf(bsBeforeTestErrorNum));

        // Integer bsBeforeTestNum = ppPatientBaseService.coutByIdsTest(ids, "105001");
        List bsBeforeTestList = ppPatientBaseService.listErrorBsIdCards(ids, "105001", null);
        Integer bsBeforeTestNum = CollUtil.isNotEmpty(bsBeforeTestList) ? bsBeforeTestList.size() : 0;
        redisUtils.set("bsBeforeTestNum", String.valueOf(bsBeforeTestNum));

        // Integer bsAfterTestErrorNum = ppPatientBaseService.countDiseasesByIdsTest(ids, "105002", null);
        List bsAfterTestErrorList = ppPatientBaseService.listErrorBsIdCards(ids, "105002", 10);
        Integer bsAfterTestErrorNum = CollUtil.isNotEmpty(bsAfterTestErrorList) ? bsAfterTestErrorList.size() : 0;
        redisUtils.set("bsAfterTestErrorNum", String.valueOf(bsAfterTestErrorNum));

        // Integer bsAfterTestNum = ppPatientBaseService.coutByIdsTest(ids, "105002");
        List bsAfterTestList = ppPatientBaseService.listErrorBsIdCards(ids, "105002", null);
        Integer bsAfterTestNum = CollUtil.isNotEmpty(bsAfterTestList) ? bsAfterTestList.size() : 0;
        redisUtils.set("bsAfterTestNum", String.valueOf(bsAfterTestNum));

        // 血糖未达标总数 2020-07-06 修改统计标准为：以最近一次测量的血糖值为标准，空腹大于等于7.0，餐后血糖大于等于10算未达标。
        Integer bsTestErrorNum = ppPatientBaseService.countBsErrorNum(ids);
        redisUtils.set("bsTestErrorNum", String.valueOf(bsTestErrorNum));
        // 血糖测量总数
        Integer bsTestNum = ppPatientBaseService.countBsNum(ids);
        redisUtils.set("bsTestNum2", String.valueOf(bsTestNum));

        Map<String, Object> bpErrorMap = ppPatientBaseService.countErrorAge(ids, "104001,104002");
        Map<String, Object> bsErrorMap = ppPatientBaseService.countErrorAge(ids, "105001,105002");

        //结果转换,方便前台显示
        List bpError = changeMap(bpErrorMap);
        redisUtils.set("bpError", JSON.toJSONString(bpError));
        List bsError = changeMap(bsErrorMap);
        redisUtils.set("bsError", JSON.toJSONString(bsError));

        Integer bpNum = ppPatientBaseService.countDiseasesByIds(ids, "1", null, null);
        redisUtils.set("bpNum", String.valueOf(bpNum));
        Integer bsNum = ppPatientBaseService.countDiseasesByIds(ids, "2", null, null);
        redisUtils.set("bsNum", String.valueOf(bsNum));
        Integer bp60Num = ppPatientBaseService.countDiseasesByIds(ids, "1", 60, null);
        redisUtils.set("bp60Num", String.valueOf(bp60Num));
        Integer bs60Num = ppPatientBaseService.countDiseasesByIds(ids, "2", 60, null);
        redisUtils.set("bs60Num", String.valueOf(bs60Num));
        Integer manNum = ppPatientBaseService.countDiseasesByIds(ids, null, null, 1);
        redisUtils.set("manNum", String.valueOf(manNum));
        Integer womanNum = ppPatientBaseService.countDiseasesByIds(ids, null, null, 0);
        redisUtils.set("womanNum", String.valueOf(womanNum));
    }

    private List changeMap(Map<String, Object> errorMap) {
        List list = new ArrayList();
        if (errorMap != null) {
            for (Map.Entry<String, Object> entry : errorMap.entrySet()) {
                Map m = new HashMap();
                m.put("age", entry.getKey());
                m.put("num", entry.getValue() == null ? 0 : entry.getValue());
                list.add(m);
            }
        }
        return list;
    }
}
